package com.ymatou.search;

import android.graphics.Rect;

import com.android.uiautomator.core.UiCollection;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.ymatou.business.ProductCardListTest;
import com.ymatou.run.UiDeviceUtil;
import com.ymatou.util.Logger;

@SuppressWarnings("deprecation")
public class SearchResultPageTest extends UiAutomatorTestCase{
	
	private static final String TAG = SearchResultPageTest.class.getSimpleName();
	
	public void testSearchResultPage() throws UiObjectNotFoundException, InterruptedException {
		
		//判断是否有搜索结果
		SearchResultPageTest.testSearchResultPageExists();
		//this.testClickFilterTip();
		//this.testTv_Search_Recommend_Keyword_Text(true);
		this.testSearch_Product_List();
		/*this.testStl_Search_Tab();
		this.testHsv_Search_Result_Other_Sort_Layout();
		
		UiSelector search_result_comprehensive_sort_s = new UiSelector().
				resourceId("com.ymatou.shop:id/tv_search_result_comprehensive_sort");
		UiObject search_result_comprehensive_sort_o = new UiObject(search_result_comprehensive_sort_s);
		search_result_comprehensive_sort_o.click();
		Logger.log(TAG, "点击" + search_result_comprehensive_sort_o.getText());*/
	}
	
	public void testClickFilterTip() throws UiObjectNotFoundException, InterruptedException {
		UiSelector tv_filter_tip_s = new UiSelector().
				resourceId("com.ymatou.shop:id/tv_filter_tip");
		
		UiObject tv_filter_tip_o = new UiObject(tv_filter_tip_s);
		tv_filter_tip_o.click();
		Logger.log(TAG, "点击" + tv_filter_tip_o.getText());
		
		new SearchResultFilterTest().testSearchResultFilter();
	}
	
	//商品、笔记、买手水平栏
	public void testStl_Search_Tab() throws UiObjectNotFoundException {
		UiSelector stl_search_tab_s = new UiSelector().
				resourceId("com.ymatou.shop:id/stl_search_tab");
		UiScrollable stl_search_tab_scroll = new UiScrollable(stl_search_tab_s);
		
		for (int i = 0; i <= stl_search_tab_scroll.getChildCount(); i++) {
			UiObject o = stl_search_tab_scroll.getChildByInstance(new UiSelector().
					className("android.widget.TextView"), i);
			Logger.log(TAG, o.getText());
			o.click();
		}
		
		UiSelector product_tab_s = new UiSelector().text("商品");
		UiObject product_tab_o = new UiObject(product_tab_s);

		product_tab_o.click();
	}
	
	//其他排序
	public void testHsv_Search_Result_Other_Sort_Layout() throws UiObjectNotFoundException {
		UiSelector hsv_search_result_other_sort_layout_s = new UiSelector().
				resourceId("com.ymatou.shop:id/hsv_search_result_other_sort_layout");
		UiScrollable hsv_search_result_other_sort_layout_scroll = new UiScrollable(hsv_search_result_other_sort_layout_s);
		
		for (int i = 0; i <= hsv_search_result_other_sort_layout_scroll.getChildCount(); i++) {
			UiObject o = hsv_search_result_other_sort_layout_scroll.getChildByInstance(new UiSelector().
					className("android.widget.TextView"), i);
			Logger.log(TAG, o.getText());
			o.click();
		}
	}
	
	//搜索推荐关键词
	public void testTv_Search_Recommend_Keyword_Text(boolean isSwipe) throws UiObjectNotFoundException {
		
		int swipe_count = 0;
		
		while (isSwipe) {
			
			UiSelector tv_search_recommend_keyword_text_s = new UiSelector().className("android.widget.LinearLayout");
			UiCollection tv_search_recommend_keyword_text_c = new UiCollection(tv_search_recommend_keyword_text_s);
			UiSelector childPattern = new UiSelector().
					resourceId("com.ymatou.shop:id/tv_search_recommend_keyword_text");
			int count = tv_search_recommend_keyword_text_c.getChildCount(childPattern);
			Rect rectStart = tv_search_recommend_keyword_text_c.getChildByInstance(childPattern, 0).getBounds();
			Rect rectrectEnd = tv_search_recommend_keyword_text_c.getChildByInstance(childPattern, count - 1).getBounds();

			String beforeSwipe_String = tv_search_recommend_keyword_text_c.getChildByInstance(childPattern, count - 1).getText();
			
			for (int i = 0; i < count; i++) {
				UiObject object = tv_search_recommend_keyword_text_c.getChildByInstance(childPattern, i);

				Logger.log(TAG, object.getText());
			}
			
			new UiDeviceUtil().swpie(rectrectEnd.left, rectrectEnd.top, rectStart.left, rectStart.top, 50);
			Logger.log(TAG, "向左滑动");
			String afterSwipe_String = tv_search_recommend_keyword_text_c.getChildByInstance(childPattern, count - 1).getText();
			
			if (beforeSwipe_String.equalsIgnoreCase(afterSwipe_String) || swipe_count == 5) {
				
				tv_search_recommend_keyword_text_c.getChildByInstance(childPattern, count - 1).click();
				break;
			}
				
			swipe_count++;
		}
	}
	
	//搜索推荐关键词
	public void testSearch_Product_List() throws UiObjectNotFoundException, InterruptedException {
		
		UiSelector gpcv_search_product_list_one_s = new UiSelector().className("android.widget.LinearLayout");
		UiCollection gpcv_search_product_list_one_c = new UiCollection(gpcv_search_product_list_one_s);
		UiSelector childPattern_product_list_one = new UiSelector().
				resourceId("com.ymatou.shop:id/gpcv_search_product_list_one");
		UiSelector childPattern_product_list_two = new UiSelector().
				resourceId("com.ymatou.shop:id/gpcv_search_product_list_two");
		
		int count = gpcv_search_product_list_one_c.getChildCount(childPattern_product_list_one);
		
		for (int i = 0; i < count; i++) {
			UiObject object_one = gpcv_search_product_list_one_c.getChildByInstance(childPattern_product_list_one, i);
			new ProductCardListTest().testCheckProductCardElement(object_one.getSelector());
			UiObject object_two = gpcv_search_product_list_one_c.getChildByInstance(childPattern_product_list_two, i);
			new ProductCardListTest().testCheckProductCardElement(object_two.getSelector());
		}
		//UiObject gpcv_search_product_list_two_o = gpcv_search_product_list_one_o.getFromParent(new UiSelector().resourceId("com.ymatou.shop:id/gpcv_search_product_list_two"));
	}
	
	public static boolean testSearchResultPageExists() {
		UiSelector search_result_comprehensive_sort_s = new UiSelector().resourceId("com.ymatou.shop:id/tv_search_result_comprehensive_sort");
	    UiObject search_result_comprehensive_sort_o = new UiObject(search_result_comprehensive_sort_s);
	    
	    return search_result_comprehensive_sort_o.waitForExists(5000);
	}
}
